﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AgileDashboard.Services;
using AgileDashboard.Entities;
using PlanningPokerMVC.Models;
using PlanningPokerMVC.Helpers;

namespace PlanningPokerMVC.Controllers
{
    public class DashboardController : Controller
    {
        //
        // GET: /Dashboard/

        public ActionResult Index()
        {




            WorkItemService workItemService = new WorkItemService();
            TfsDataLoaderService service = new TfsDataLoaderService();

            //flat query
            //Guid queryId = new Guid("8b131cc0-abe2-454d-be45-4219f37af7bc");

            //hiearchical query
            //Guid queryId = new Guid("2701b79f-61e9-4345-9655-a6210f5bf410");

            //single epic query
            Guid queryId = new Guid("450c5e0f-3677-4d74-b428-eafcf94b5fba");


            List<WorkItem> workItems = workItemService.GetWorkItems(queryId, 499, true);


            DashboardViewModel viewModel = new DashboardViewModel();
            viewModel.WorkItems = workItems;


            return View(viewModel);


        }



        public JsonResult EpicReport()
        {
            WorkItemService workItemService = new WorkItemService();
            TfsDataLoaderService service = new TfsDataLoaderService();

            //flat query
            Guid queryId = new Guid("8b131cc0-abe2-454d-be45-4219f37af7bc");

            //hiearchical query
            //Guid queryId = new Guid("2701b79f-61e9-4345-9655-a6210f5bf410");

            //single epic query
            //Guid queryId = new Guid("450c5e0f-3677-4d74-b428-eafcf94b5fba");


            List<WorkItem> workItems = workItemService.GetWorkItems(queryId, 499, true);

            WorkItemReportingService reportingService = new WorkItemReportingService();

            EpicReportViewModel epicReport = new EpicReportViewModel(reportingService.CalculateEpicStoryPointSummary(workItems));


            return Json(epicReport.EpicReport,JsonRequestBehavior.AllowGet);
        }


        public JsonResult WorkItems()
        {



            WorkItemService workItemService = new WorkItemService();
            TfsDataLoaderService service = new TfsDataLoaderService();

            //flat query
            //Guid queryId = new Guid("8b131cc0-abe2-454d-be45-4219f37af7bc");

            //hiearchical query
            //Guid queryId = new Guid("2701b79f-61e9-4345-9655-a6210f5bf410");

            //single epic query
            Guid queryId = new Guid("450c5e0f-3677-4d74-b428-eafcf94b5fba");


            List<WorkItem> workItems = workItemService.GetWorkItems(queryId, 499, true);


            DashboardViewModel viewModel = new DashboardViewModel();
            viewModel.WorkItems = workItems;

    
            return Json(workItems, JsonRequestBehavior.AllowGet);



        }





        //
        // GET: /Dashboard/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Dashboard/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Dashboard/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Dashboard/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Dashboard/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Dashboard/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Dashboard/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
